<?php

require_once('Yeti/Defines.php');
require_once('Yeti/Elegance.php');

final class Yeti {
    static $loaded = Array(), $list_config = Array(), 
        $x = 1;
    
    
    final public static function autoload($class) {
        if(class_exists($class, FALSE) OR interface_exists($class, FALSE)) {
            return TRUE;
        }
        
        $underscore = explode('_', preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class));
        $klass = array_reverse($underscore);

        array_shift($klass);
        
        return self::import($klass, 'Yeti');
    }
    
    final public static function config($conf) {
        if(empty(self::$list_config)) {
            require("Home/Config/Config.php");
            
            foreach($config as $module => $arr) {
                foreach($arr as $_config => $value) {
                    self::$list_config["{$module}.{$_config}"] = $value;
                }
            }
        }
        
        return (isset(self::$list_config[$conf]) ? self::$list_config[$conf] : FALSE);
    }
    
    public static function import($class, $local = 'Home', $layer = NULL) {
        if(is_string($class)) {
            $class = array_map('ucwords', explode('.', $class));
        }
        
        $local .= DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $class);
        
        if(isset(self::$loaded[$local])) {
            return FALSE;
        }

        $file = ROOT_YETI . $local . ($layer ? DIRECTORY_SEPARATOR . ucwords($layer) : "") . ".php";
        
        //echo self::$x++ . ' - ' . $file . '<br />';
        
        /*if(!is_file($file)) {
            return FALSE;
        }*/
    
        self::$loaded[$file] = microtime(true);
        require_once($file);

        return TRUE;
    }
    
    public static function singleton($class) {
        
    }
    
    public static function factory($class) {
        
    }
    
    final public static function normalizeOptionsIsExec($opts, $arg) {
        if(empty($opts)) {
            return TRUE;
        }
        elseif(isset($opts['only']) AND in_array($arg, $opts['only'])) {
            return TRUE;
        }
        elseif(isset($opts['except']) AND !in_array($arg, $opts['except'])) {
            return TRUE;
        }
        
        return FALSE;
    }
}
